package com.qilin.hscs.hm.mqtt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.qilin.hscs.hm.mqtt.service.SceneDeviceService;
import com.qilin.hscs.hm.mqtt.service.common.ServiceCommon;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

/**
 * Created by 10188 on 2018/4/30.
 */
@Service("sceneDevice")
public class SceneDeviceServiceImpl extends ServiceCommon implements SceneDeviceService {

    private static final Logger logger= LogManager.getLogger(SceneDeviceServiceImpl.class.getName());

    @Override
    public void findTotalDeviceInScene() {

    }

    @Override
    public void addDeviceInScene() {

    }

    @Override
    public void alterDeviceInScene() {

    }

    @Override
    public void delDeviceInScene() {

    }

    @Override
    public void excuteScene() {  //todo

    }

    @Override
    public void bindScene() {

    }

    @Override
    public void handle() {
        switch (cmd) {
            case 10001:
                findTotalDeviceInScene();
                isflushToGateway(false);
                flush();
                break;
            case 10002:
                isflushToGateway(true);
                flush();
                break;
            case 10003:
                isflushToGateway(true);
                flush();
                break;
            case 14003:  //todo
                isflushToGateway(true);
                flush();
                break;
            case 10004:
                isflushToGateway(true);
                flush();
                break;
            case 10005:
                isflushToGateway(true);
                flush();
                break;
            case 11002:
                addDeviceInScene();
                isflushToGateway(false);
                flush();
                break;
            case 11003:
                alterDeviceInScene();
                isflushToGateway(false);
                flush();
                break;
            case 15003://todo
                delDeviceInScene();
                isflushToGateway(false);
                flush();
                break;
            case 11004:
                bindScene();
                isflushToGateway(false);
                flush();
                break;
            default:
        }
    }

    @Override
    public void initailData() {
        try {
            msgObj = JSON.parseObject(msg);
            dataObj = msgObj.getJSONObject("data");
        } catch (JSONException je) {
            logger.warn("传输过来的json字符串有误，主机无法解析 错误详细情况为={}", je.getStackTrace());
        }
        cmd = msgObj.getInteger("cmd");
        if (topicVar != null) {
            if (!topicVar.contains("/rsp")) {
                phone = msgObj.getString("phone");
            }
        }
    }
}
